(function(){
 var alldata = {
        "paymentoptType": [{
            id: "0",
            name: "微信",
            en: "weixin",
        }, {
            id: "1",
            name: "支付宝",
            en: "payment",
        }]
    };

    _.map(alldata, function(zdata, dname) {
        angular.module('webApp').directive(dname, ['dataService', '$compile',
            function(dataService, $compile) {
                return {
                    restrict: 'E',
                    require: 'ngModel',
                    scope: {
                        ngModel: '=',
                        stype: "@", //是否只输出文字
                        dft: "@" //是否设置初始化值
                    },

                    template: '<div class="aui-list-item-input"><select ng-model="tmpNgModel" ng-options="option.name for option in elmAry track by option.key"></select></div>',
                    controller: ['$templateCache', '$rootScope', '$scope', '$timeout',
                        function($templateCache, $rootScope, $scope, $timeout) {
                            $scope.tmpNgModel = {};
                            $scope.show = function() {
                                if (window.localStorage.lang == 'zh-cn' || !window.localStorage.lang) {
                                    return $scope.tmpNgModel.name || $scope.ngModel;
                                } else {
                                    return $scope.tmpNgModel.en || $scope.ngModel;
                                }
                            }

                            function initData() {

                                $scope.elmAry = {};
                                var n = 1;
                                _.map(zdata, function(v, k) {
                                    v.key = "v" + n;
                                    $scope.elmAry[v.key] = v;
                                    n++;
                                });
                                console.log("-----",$scope.elmAry);
                            }
                            initData();

                            $scope.checkValue = function(id) {
                                _.map($scope.elmAry, function(a) {
                                    if (a.id == id) {
                                        $scope.tmpNgModel = a;
                                    }
                                });
                            }
                        }
                    ],

                    link: function(scope, element, attrs, ctrl) {
                        scope.$watch('tmpNgModel', function(newValue, oldValue, scope) {
                            if (newValue) {
                                ctrl.$setViewValue(newValue.id);
                            }
                        });
                        console.log("111111111");
                        scope.$watch('elmAry', function(newValue, oldValue, scope) {
                            if (!ctrl.$viewValue && attrs.dft && newValue) {
                                var x = _.values(newValue);
                                if (x[0]) {
                                    scope.tmpNgModel = x[0];
                                    ctrl.$setViewValue(x[0].id);
                                }
                            }
                        });

                        //初始化
                        ctrl.$render = function() {
                            scope.checkValue(ctrl.$viewValue);
                        };
                    }
                }
            }
        ]);
    });
})();